<!--
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-21 10:45:02
 * @LastEditTime: 2022-08-04 16:38:18
-->
<template>
  <div class="subscription-list">
    <dt-grid ref="table" :page="page" :onFetch='onFetch'>
      <div class="flex btn-content" slot="btn">
        <Button type="primary" @click="adjustOrder()" v-if="adjustBtnFlag"
          style="margin-right:8px;" :loading="loading">发起调整</Button>
        <Button type="primary" @click="exportFun()" v-if="exportBtnFlag"
          style="margin-right:8px;">导出</Button>
      </div>
    </dt-grid>
    <!--调整附件-->
    <adjust-enclosure ref="adjustEnclosure"></adjust-enclosure>
    <!--流程信息-->
    <process-modal ref="process" />
  </div>
</template>
<script>
  import SubColumn from './opt/columnSub'
  import ConColumn from './opt/columnCon'
  import adjustMgr from '@/biz/sales/adjustOrder'
  import adjustEnclosure from '../components/adjustEnclosure.vue'
  import processModal from '@/components/biz/audit/process-modal.vue'
  import auditMgr from '@/biz/flowApproval/processAudit.js'
  import dt from '@dt/dt'
  export default {
    props: ['adjustType'],
    data() {
      return {
        filterParams: {},
        page: {
          rows: 50,
          current: 1
        },
        loading: false,
        adjustBtnFlag: false,
        exportBtnFlag: false
      }
    },
    components: {
      adjustEnclosure,
      processModal
    },
    created() {},
    mounted() {
      let _this = this
      let retainParams = {}
      if (this.adjustType == 'SUBSCRIBER') {
        this.adjustBtnFlag = this.unlocking('0104010401')
        this.exportBtnFlag = this.unlocking('0104010402')

        retainParams = dt.memory.get('subAdjust_params')
        this.$refs.table.init(this, new SubColumn(this))
      } else {
        this.adjustBtnFlag = this.unlocking('0104030401')
        this.exportBtnFlag = this.unlocking('0104030402')

        retainParams = dt.memory.get('conAdjust_params')
        this.$refs.table.init(this, new ConColumn(this))
      }

      this.$nextTick(function() {
        if (retainParams) {
          retainParams.rows && (this.page.rows = retainParams.rows)
          retainParams.page && (this.page.current = retainParams.page)
          retainParams.filter && _this.$refs.table.throwFilter().import(
            retainParams.filter)
        }
      })
      this.$refs.table.fetch()
    },
    methods: {
      retainParamsFun() {
        let params = Object.assign({
          filter: this.$refs.table.throwFilter().export()
        }, this.filterParams)

        if (this.adjustType == 'SUBSCRIBER') {
          dt.memory.set('subAdjust_params', params)
        } else {
          dt.memory.set('conAdjust_params', params)
        }
      },
      adjustOrder() {
        let params = {
          adjustType: this.adjustType,
          housesId: dt.buildInfo().housesId
        }
        this.loading = true
        adjustMgr.applyAdjustmentFun(params).then(res => {
          this.loading = false
          this.$router.push({
            name: (this.adjustType == 'SUBSCRIBER') ? 'subAdjust' : 'conAdjust',
            query: {
              adjustmentId: res
            }
          })
        }).catch(err => {
          this.loading = false
        })
      },
      onFetch(data) {
        let params = {
          ...data,
          housesId: dt.buildInfo().housesId,
          adjustType: this.adjustType,
          ...this.page
        }

        this.filterParams = {
          ...params
        }

        return adjustMgr.getSelectAdjustmentList(params).then(res => {
          return res
        }).catch(err => {})
      },
      preViewFile(row) {
        this.$refs.adjustEnclosure.show(row)
      },
      // 流程信息
      viewProcessFun(row) {
        this.$refs.process.show({
          businessId: row.adjustmentId,
          auditType: (this.adjustType == 'SUBSCRIBER') ? 10 : 11
        })
      },
      // 导出
      exportFun() {
        let params = { ...this.filterParams }
        adjustMgr
          .exportAdjustmentFun(params)
          .then((res) => {})
          .catch((err) => {})
      },
      // 删除
      delAdjustFun(row) {
        this.$Modal.confirm({
          title: '删除',
          content: `你确认删除此${(this.adjustType == 'SUBSCRIBER')?'认购':'签约'}调整单么？`,
          closable: true,
          onOk: () => {
            let params = {
              adjustmentId: row.adjustmentId
            }
            adjustMgr.delAdjustmentFun(params).then(res => {
              this.$Message.success('删除成功')
              this.$refs.table.fetch()
            }).catch(err => {})
          }
        })
      },
      // 撤销调整
      cancleAdjustFun(row) {
        this.$Modal.confirm({
          title: '撤销调整',
          content: `你确认撤销此${(this.adjustType == 'SUBSCRIBER')?'认购':'签约'}调整单么？`,
          closable: true,
          onOk: () => {
            let params = {
              auditType: (this.adjustType == 'SUBSCRIBER') ? 10 : 11,
              businessId: row.adjustmentId,
            }
            auditMgr.back(params).then((res) => {
              this.$Message.success('撤销成功')
              this.$refs.table.fetch()
            })
          }
        })
      },
      // 编辑
      editFun(row) {
        this.$router.push({
          name: (this.adjustType == 'SUBSCRIBER') ? 'subAdjust' : 'conAdjust',
          query: {
            adjustmentId: row.adjustmentId
          }
        })
      },
      //详情
      detailFun(row) {
        // 认购调整: 'TR',
        // 签约调整: 'TQ',
        let orderParams = {}
        let type = row.adjustNo.slice(0, 2)
        switch (type) {
          case 'TR':
            orderParams = {
              orderNum: row.adjustNo,
              businessId: row.adjustmentId, // 单号
              orderType: '认购'
            }
            break
          case 'TQ':
            orderParams = {
              orderNum: row.adjustNo,
              businessId: row.adjustmentId, // 单号
              orderType: '签约'
            }
            break
          default:
            break
        }
        this.linkOrderFun(orderParams)
      }
    },
  }
</script>
<style lang="scss" scoped>
  .subscription-list {
    padding: 20px;
  }
</style>